package com.web;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.entity.User;
import com.service.UserService;
import com.service.impl.UserserviceImpl;



/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("op");
		
		if(method.equals("registUser")){
			registUser(request,response);
		}else if(method.equals("checkName")) {
			checkName(request,response);
		}else if(method.equals("login")) {
			login(request,response);
		}else if(method.equals("logout")) {
			logout(request,response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	private void registUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uuid = UUID.randomUUID().toString().replace("_", "");
		String username = request.getParameter("username");
		String realname = request.getParameter("userName");
		String password = request.getParameter("password");
		String gender = request.getParameter("sex");
		Date birthday=null;
		try {
			birthday = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("birthday"));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String identityCode = request.getParameter("identityCode");
		String email = request.getParameter("email");
		String mobile = request.getParameter("mobile");
		String address = request.getParameter("address");
		Date regdate = new Date();
		System.out.println("前台传输数据接收成功");
		User user = new User(uuid,username,realname,password,gender,identityCode,email,mobile,address,"user",1,birthday,regdate);
		UserService us = new UserserviceImpl();
		try {
			boolean recorder = us.registUser(user);
			if(recorder) {
				response.sendRedirect("login.jsp");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void checkName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		UserService us = new UserserviceImpl();
		boolean result = us.checkName(username);
		if(result) {
			response.getWriter().write("账户名可用");
		}else {
			response.setCharacterEncoding("utf-8");
			response.getWriter().write("账户名不可以用");
			
		}
	}
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String checkCode = request.getParameter("checkCode");
		HttpSession session = request.getSession();
		if(checkCode.equals(session.getAttribute("randCheckCode"))) {
			String username = request.getParameter("userId");
			String password = request.getParameter("password");
			User users =  new User();
			users.setUsername(username);
			users.setPassword(password);
			UserService us = new UserserviceImpl();
			//查询到的数据放到session中
			User user = us.login(users);
			session.setAttribute("user", user);
			response.setCharacterEncoding("utf-8");
			response.sendRedirect(request.getContextPath()+"/index.jsp");
		}else{
			//将验证码错误信息写回到前端页面
		}		
	}
	
	private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		response.sendRedirect("index.jsp");
	}

}
